<?php
/* explain:		logs class
 * Project:     marsems
 * File:        Logs.php
 *
 * @ link 		http://www.../lib/
 * @ Email		ldmmyx@hotmail.com
 * @ copyright 	2007  Ling Deming	
 * @ author 	Ivan  Ling
 * @ version 3.0
 *
 */
include_once("Common.php");
include_once("domain/LogRequestFeed.php");

class LogImpl extends Common
{
	private $conn;
	
	private $DBTag;
	private $message;
	
	function LogImpl($DBTag)
	{
		parent::Common();
		
		$this->DBTag 	= $DBTag;
		$this->conn 	= $this->getConnection($DBTag);	
	}
	
	public function add($vo)
	{
		if($vo instanceof LogRequestFeed){			
			$sql 	 = "INSERT INTO log_request_feed SET sourceId = ";
			$sql	.= $vo->getSourceId();
			
			$sql 	.= ", startTime = '";
			$sql	.= $vo->getStartTime();
			
			$sql 	.= "', status = ";
			$sql	.= $vo->getStatus();
			
			$id 	= 0;
			
			try{
				$rs = mysql_query($sql, $this->conn);
				if($rs){
					$id = mysql_insert_id($this->conn);
					
					$vo->setId($id);
										
					$this->messages = "add successfully.";
				}else{
					$this->messages  = "add failed. sql : $sql . <br>";
					$this->messages .= "error: " .  mysql_error($this->conn);
				}
			}catch(Exception $e){
				$this->messages = $e->getMessage();
				
				$id	= -1;
			}
		}else{
			$id	= -1;
			
			$this->message = 'insert object is not instance of LogRequestFeed.';
		}
			
		return $id;
	}

	public function setEndTime($vo)
	{
		if($vo instanceof LogRequestFeed){
			$sql  	 = "UPDATE log_request_feed SET endTime = now(), status = ";
			$sql	.= $vo->getStatus();
			
			$sql 	.= " WHERE id = ";
			$sql	.= $vo->getId();
			return $this->update($this->DBTag, $sql);			
		}
		
		return false;
	}
	
	public function getLastRequestTime($sourceId)
	{
		$sql = "SELECT endTime FROM log_request_feed WHERE status = 2 ORDER BY endTime DESC LIMIT 1";
				
		$endTime = $this->getColumn($this->DBTag, $sql);
		
		if(empty($endTime)){
			$sql = "SELECT CURTIME()";
				
			$endTime = $this->getColumn($this->DBTag, $sql);
		}
		
		return $endTime;
	}
	
}
?>
